package com.xbongbong.paas.help.team;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.message.constant.BomPushConstant;
import com.xbongbong.pro.message.constant.CluePushConstant;
import com.xbongbong.pro.message.constant.ContractPushConstant;
import com.xbongbong.pro.message.constant.CustomerPushConstant;
import com.xbongbong.pro.message.constant.DistributorPushConstant;
import com.xbongbong.pro.message.constant.OpportunityPushConstant;
import com.xbongbong.pro.message.constant.OrderPushConstant;
import com.xbongbong.pro.message.constant.ProductionOrderPushConstant;
import com.xbongbong.pro.message.constant.PurchasePushConstant;
import com.xbongbong.pro.message.constant.QuotationPushConstant;
import com.xbongbong.pro.message.constant.SupplierPushConstant;
import com.xbongbong.pro.message.constant.WorkOrderPushConstant;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.help.PushHelper;
import com.xbongbong.pro.message.model.PushSetModel;
import com.xbongbong.pro.message.pojo.PushRefTypePojo;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pushset.CluePushSet;
import com.xbongbong.pro.message.pushset.ContractPushSet;
import com.xbongbong.pro.message.pushset.CustomerPushSet;
import com.xbongbong.pro.message.pushset.OpportunityPushSet;
import com.xbongbong.pro.message.pushset.OrderPushSet;
import com.xbongbong.pro.message.pushset.QuotationPushSet;
import com.xbongbong.pro.message.pushset.WorkOrderPushSet;
import com.xbongbong.pro.rabbitmq.producer.BaseProducer;
import com.xbongbong.pro.team.TeamAfterVerifyDTO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.BomUserEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderUserEntity;
import com.xbongbong.saas.domain.entity.PurchaseUserEntity;
import com.xbongbong.saas.domain.entity.SupplierUserEntity;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.enums.business.SupplierEnum;
import com.xbongbong.saas.help.SaasGetHelp;
import com.xbongbong.saas.model.BomUserModel;
import com.xbongbong.saas.model.ProductionOrderUserModel;
import com.xbongbong.saas.model.PurchaseUserModel;
import com.xbongbong.saas.model.SupplierUserModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.model.UserModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Component
public class TeamMessageHelper {

    @Resource
    private BaseProducer baseProducer;
    @Resource
    private BomUserModel bomUserModel;
    @Resource
    private ProductionOrderUserModel productionOrderUserModel;
    @Resource
    private PurchaseUserModel purchaseUserModel;
    @Resource
    private PushHelper pushHelper;
    @Resource
    private PushSetModel pushSetModel;
    @Resource
    private SaasGetHelp saasGetHelp;
    @Resource
    private SupplierUserModel supplierUserModel;
    @Resource
    private UserModel userModel;


    /**
     *
     * @Description: 团队添加负责人/协同人推送
     * @param teamAfterVerifyDTO
     * @param isMain
     * @param saveName
     * @param addUserSet 新增负责人/新增协同人需要推送的人
     * @param paasFormDataEntity
     * @return: void
     * @Author: shen.yang
     * @Date: 2022/1/12  19:18
     */
    public void addUserPush(TeamAfterVerifyDTO teamAfterVerifyDTO, boolean isMain, String saveName, Set<String> addUserSet, PaasFormDataEntity paasFormDataEntity) throws XbbException {
        Integer businessType = teamAfterVerifyDTO.getBusinessType();
        String corpId = teamAfterVerifyDTO.getCorpid();
        Long dataId = paasFormDataEntity.getDataId();
        Integer distributorMark = teamAfterVerifyDTO.getDistributorMark();
        UserVO operator = teamAfterVerifyDTO.getLoginUser();
        if (Objects.isNull(paasFormDataEntity) || Objects.isNull(operator) || CollectionsUtil.isEmpty(addUserSet)) {
            return;
        }
        // 业务数据的名称
        String businessDataName = "";
        // 推送类型
        PushTypeEnum pushTypeEnum = null;
        // 推送标题
        String title = "";
        // 推送给添加的人员的内容
        String userContent = "";
        // 推送给消息设置的人员的内容
        String pushContent = "";
        // 推送人员
        List<String> pushUserList = new ArrayList<>();
        businessType = XbbRefTypeEnum.transferBusinessType4Distributor(businessType, distributorMark);
        UserTeamEnum userTeamEnum = UserTeamEnum.getByCode(businessType);
        switch (userTeamEnum) {
            case CUSTOMER_TEAM:
                businessDataName = paasFormDataEntity.getData().getString(CustomerManagementEnum.NAME.getAttr());
                pushTypeEnum = isMain ? PushTypeEnum.CUSTOMER_TEAM_ADD_DEL_MAIN : PushTypeEnum.CUSTOMER_TEAM_ADD_DEL_SYNERGETIC;
                title = isMain ? I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_ADD_OTHER_CONTENT);
                break;
            case CONTRACT_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.CONTRACT_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.CONTRACT_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_ADD_OTHER_CONTENT);
                break;
            case ORDER_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.ORDER_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_ADD_OTHER_CONTENT);
                break;
            case OPPORTUNITY_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_ADD_OTHER_CONTENT);
                break;
            case SUPPLIER_TEAM:
                businessDataName = paasFormDataEntity.getData().getString(SupplierEnum.SUPPLIER_NAME.getAttr());
                pushTypeEnum = isMain ? PushTypeEnum.SUPPLIER_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.SUPPLIER_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_ADD_OTHER_CONTENT);
                List<SupplierUserEntity> supplierUserList = supplierUserModel.getSupplierUserListBySupplierId(corpId, dataId);
                for (SupplierUserEntity item : supplierUserList) {
                    pushUserList.add(item.getUserId());
                }
                break;
            case PURCHASE_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.PURCHASE_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.PURCHASE_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_ADD_OTHER_CONTENT);
                List<PurchaseUserEntity> purchaseUserEntities = purchaseUserModel.getPurchaseUserListByPurchaseId(corpId, dataId, null);
                for (PurchaseUserEntity entity : purchaseUserEntities) {
                    pushUserList.add(entity.getUserId());
                }
                break;
            case BOMBILL_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.BOMBILL_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.BOMBILL_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_ADD_OTHER_CONTENT);
                List<BomUserEntity> bomUserList = bomUserModel.getBomUserListByBomId(corpId, dataId);
                for (BomUserEntity item : bomUserList) {
                    pushUserList.add(item.getUserId());
                }
                break;
            case PRODUCTIONORDER_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.PRODUCTION_ORDER_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.PRODUCTION_ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_ADD_OTHER_CONTENT);
                List<ProductionOrderUserEntity> productionOrderUserEntityList = productionOrderUserModel.getProductionOrderUserListByProductionOrderId(corpId, dataId);
                for (ProductionOrderUserEntity item : productionOrderUserEntityList) {
                    pushUserList.add(item.getUserId());
                }
                break;
            case WORK_ORDER_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                businessDataName = businessDataName == null ? saveName : businessDataName;
                pushTypeEnum = PushTypeEnum.WORKORDER_TEAM;
                title = I18nMessageUtil.getMessage(WorkOrderPushConstant.WORK_ORDER_TEAM_ADD_TITLE);
                userContent = I18nMessageUtil.getMessage(WorkOrderPushConstant.TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(WorkOrderPushConstant.TEAM_ADD_OTHER_CONTENT);
                break;
            case QUOTATION_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.QUOTATION_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.QUOTATION_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_ADD_OTHER_CONTENT);
                break;
            case CLUE_TEAM:
                businessDataName = FastJsonHelper.getStringOrDefaultFromFormData(paasFormDataEntity.getData(), ClueEnum.COMPANY_NAME.getAttr(), "");
                pushTypeEnum = isMain ? PushTypeEnum.CLUE_TEAM_ADD_DEL_MAIN : PushTypeEnum.CLUE_TEAM_ADD_DEL_SYNERGETIC;
                title = isMain ? I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_ADD_OTHER_CONTENT);
                break;
            case DISTRIBUTOR_TEAM:
                businessDataName = paasFormDataEntity.getData().getString(DistributorEnum.NAME.getAttr());
                pushTypeEnum = isMain ? PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_MAIN : PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC;
                title = isMain ? I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_ADD_MAIN_TITLE) : I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_ADD_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_ADD_CONTENT);
                pushContent = I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_ADD_OTHER_CONTENT);
                break;
            default:
                break;
        }

        if (Objects.nonNull(pushTypeEnum) && !Objects.equals(userTeamEnum, UserTeamEnum.UNKNOW)) {
            businessType = XbbRefTypeEnum.transferBusinessType2Index(businessType);
            PushSetEntity pushSetEntity = null;
            if (Objects.equals(PushTypeEnum.CUSTOMER_PUSH.getCode(), pushTypeEnum.getCode())) {
                CustomerPushSet customerPushSet = new CustomerPushSet();
                pushSetEntity = customerPushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            } else if (Objects.equals(PushTypeEnum.CONTRACT_PUSH.getCode(), pushTypeEnum.getCode())) {
                ContractPushSet contractPushSet = new ContractPushSet();
                pushSetEntity = contractPushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            } else if (Objects.equals(PushTypeEnum.ORDER_PUSH.getCode(), pushTypeEnum.getCode())) {
                OrderPushSet orderPushSet = new OrderPushSet();
                pushSetEntity = orderPushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_PUSH.getCode(), pushTypeEnum.getCode())) {
                OpportunityPushSet opportunityPushSet = new OpportunityPushSet();
                pushSetEntity = opportunityPushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            } else if (Objects.equals(PushTypeEnum.WORKORDER_PUSH.getCode(), pushTypeEnum.getCode())) {
                WorkOrderPushSet workOrderPushSet = new WorkOrderPushSet();
                pushSetEntity = workOrderPushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            } else if (Objects.equals(PushTypeEnum.QUOTATION_PUSH.getCode(), pushTypeEnum.getCode())) {
                QuotationPushSet quotationPushSet = new QuotationPushSet();
                pushSetEntity = quotationPushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            } else if (Objects.equals(PushTypeEnum.CLUE_PUSH.getCode(), pushTypeEnum.getCode())) {
                CluePushSet cluePushSet = new CluePushSet();
                pushSetEntity = cluePushSet.getPushSet(corpId, pushTypeEnum.getSubCode(), pushSetModel);
            }
            boolean isEnable = true;
            if (Objects.nonNull(pushSetEntity)) {
                if (Objects.equals(pushSetEntity.getEnable(), 0)) {
                    isEnable = false;
                }
            }
            if (Objects.equals(userTeamEnum, UserTeamEnum.WORK_ORDER_TEAM)) {
                WorkOrderEntity workOrderEntity = new WorkOrderEntity();
                BeanUtil.copyProperties(paasFormDataEntity, workOrderEntity);
                pushUserList = pushHelper.getUserIdListByPushSetRule(corpId, pushTypeEnum.getCode(), pushTypeEnum.getSubCode(), operator, workOrderEntity);
            } else if (!Objects.equals(userTeamEnum, UserTeamEnum.SUPPLIER_TEAM) && !Objects.equals(userTeamEnum, UserTeamEnum.BOMBILL_TEAM)
                    && !Objects.equals(userTeamEnum, UserTeamEnum.PRODUCTIONORDER_TEAM) && !Objects.equals(userTeamEnum, UserTeamEnum.PURCHASE_TEAM)) {
                // 从设置中获取推送人员
                pushUserList = pushHelper.getUserIdListByPushSetRule(corpId, pushTypeEnum.getCode(), pushTypeEnum.getSubCode(), operator, paasFormDataEntity);
            }
            if (isEnable) {
                PushRefTypePojo.Options options = new PushRefTypePojo.Options(paasFormDataEntity.getAppId(), paasFormDataEntity.getMenuId(), paasFormDataEntity.getFormId(), dataId, Collections.singletonList(dataId), SaasMarkEnum.SAAS.getCode(), businessType, businessType);
                if (!(addUserSet.contains(operator.getUserId()) && Objects.equals(addUserSet.size(), 1))) {
                    if (userTeamEnum.equals(UserTeamEnum.WORK_ORDER_TEAM)) {
                        userContent = String.format(userContent, operator.getName(), businessDataName);
                    } else {
                        userContent = String.format(userContent, businessDataName);
                    }
                    addUserSet.remove(operator.getUserId());
                    MessageRabbitMqDTO messageRabbitMqDTO = new MessageRabbitMqDTO(corpId, new ArrayList<>(addUserSet), Collections.singletonList(dataId), 0, null, title, userContent, null, options);
                    baseProducer.sendMessage(pushTypeEnum, messageRabbitMqDTO);
                }
            }
            if (pushUserList != null && pushUserList.size() > 0) {
                PushRefTypePojo.Options options = new PushRefTypePojo.Options(paasFormDataEntity.getAppId(), paasFormDataEntity.getMenuId(), paasFormDataEntity.getFormId(), dataId, Collections.singletonList(dataId), SaasMarkEnum.SAAS.getCode(), businessType, businessType);
                Map<String, String> userIdNameMap = userModel.getUserIdNameMap(corpId, addUserSet);
                StringBuilder teamUserNameSb = new StringBuilder();
                for (String name : userIdNameMap.values()) {
                    teamUserNameSb.append(name).append(StringConstant.COMMA);
                }
                String teamUserName = teamUserNameSb.toString();
                if (StringUtil.isNotEmpty(teamUserName)) {
                    teamUserName = teamUserName.substring(0, teamUserName.length() - 1);
                }
                if (userTeamEnum.equals(UserTeamEnum.WORK_ORDER_TEAM)) {
                    pushContent = String.format(pushContent, operator.getName(), teamUserName, businessDataName);
                } else {
                    pushContent = String.format(pushContent, teamUserName, businessDataName);
                }
                //过滤被加的人员
                pushUserList.removeAll(new ArrayList<>(addUserSet));
                //过滤当前操作人
                pushUserList.remove(operator.getUserId());
                MessageRabbitMqDTO messageRabbitMqPushSetDTO = new MessageRabbitMqDTO(corpId, pushUserList, Collections.singletonList(dataId), 0, null, title, pushContent, null, options);
                baseProducer.sendMessage(pushTypeEnum, messageRabbitMqPushSetDTO);
            }


        }
    }

    /**
     *
     * @Description: 删除负责人/协同人推送
     * @param teamAfterVerifyDTO
     * @param isMain
     * @param saveName
     * @param delUserSet 需要删除的用户
     * @param paasFormDataEntity
     * @return: void
     * @Author: shen.yang
     * @Date: 2022/1/12  19:52
     */
    public void delUserPush(TeamAfterVerifyDTO teamAfterVerifyDTO, boolean isMain,  String saveName, Set<String> delUserSet, PaasFormDataEntity paasFormDataEntity) throws XbbException {
        Integer businessType = teamAfterVerifyDTO.getBusinessType();
        String corpid = teamAfterVerifyDTO.getCorpid();
        Long dataId = paasFormDataEntity.getDataId();
        Integer distributorMark = teamAfterVerifyDTO.getDistributorMark();
        UserVO operator = teamAfterVerifyDTO.getLoginUser();
        if (Objects.isNull(paasFormDataEntity) || CollectionsUtil.isEmpty(delUserSet)) {
            return;
        }
        //这边需要重新转化一下，因为下面查询推送人的时候，必须要是业务实体，导致得查具体的业务。所以看到paasFormDataEntity传进来又去查一次不要疑惑
        paasFormDataEntity = saasGetHelp.getFormData(dataId, corpid, businessType);
        // 业务数据的名称
        String businessDataName = "";
        // 推送类型
        PushTypeEnum pushTypeEnum = null;
        // 推送标题
        String title = "";
        // 推送给删除的人员的内容
        String userContent = "";
        // 推送给消息设置的人员的内容
        String pushContent = "";
        // 推送人员
        List<String> pushUserList = new ArrayList<>();
        // 协同团队类型,经销商需转businessType
        businessType = XbbRefTypeEnum.transferBusinessType4Distributor(businessType, distributorMark);
        UserTeamEnum userTeamEnum = UserTeamEnum.getByCode(businessType);
        switch (userTeamEnum) {
            case CUSTOMER_TEAM:
                businessDataName = paasFormDataEntity.getData().getString(CustomerManagementEnum.NAME.getAttr());
                pushTypeEnum = isMain ? PushTypeEnum.CUSTOMER_TEAM_ADD_DEL_MAIN : PushTypeEnum.CUSTOMER_TEAM_ADD_DEL_SYNERGETIC;
                title = isMain ? I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(CustomerPushConstant.CUSTOMER_TEAM_DEL_OTHER_CONTENT);
                break;
            case CONTRACT_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.CONTRACT_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.CONTRACT_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(ContractPushConstant.CONTRACT_TEAM_DEL_OTHER_CONTENT);
                break;
            case ORDER_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.ORDER_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(OrderPushConstant.ORDER_TEAM_DEL_OTHER_CONTENT);
                break;
            case QUOTATION_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.QUOTATION_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.QUOTATION_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(QuotationPushConstant.QUOTATAION_TEAM_DEL_OTHER_CONTENT);
                break;
            case OPPORTUNITY_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(OpportunityPushConstant.OPPORTUNITY_TEAM_DEL_OTHER_CONTENT);
                break;
            case SUPPLIER_TEAM:
                businessDataName = paasFormDataEntity.getData().getString(SupplierEnum.SUPPLIER_NAME.getAttr());
                pushTypeEnum = isMain ? PushTypeEnum.SUPPLIER_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.SUPPLIER_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(SupplierPushConstant.SUPPLIER_TEAM_DEL_OTHER_CONTENT);
                List<SupplierUserEntity> supplierUserList = supplierUserModel.getSupplierUserListBySupplierId(corpid, dataId);
                for (SupplierUserEntity item : supplierUserList) {
                    pushUserList.add(item.getUserId());
                }
                break;
            case PURCHASE_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.PURCHASE_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.PURCHASE_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(PurchasePushConstant.PURCHASE_TEAM_DEL_OTHER_CONTENT);
                List<PurchaseUserEntity> purchaseUserEntities = purchaseUserModel.getPurchaseUserListByPurchaseId(corpid, dataId, null);
                for (PurchaseUserEntity entity : purchaseUserEntities) {
                    pushUserList.add(entity.getUserId());
                }
                break;
            case BOMBILL_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.BOMBILL_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.BOMBILL_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_DEL_MAIN_TITLE): I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(BomPushConstant.BOM_TEAM_DEL_OTHER_CONTENT);
                List<BomUserEntity> bomUserList = bomUserModel.getBomUserListByBomId(corpid, dataId);
                for (BomUserEntity item : bomUserList) {
                    pushUserList.add(item.getUserId());
                }
                break;
            case PRODUCTIONORDER_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                pushTypeEnum = isMain ? PushTypeEnum.PRODUCTION_ORDER_TEAM_ADD_DEL_MAIN_PUSH : PushTypeEnum.PRODUCTION_ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH;
                title = isMain ? I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(ProductionOrderPushConstant.PRODUCTION_ORDER_TEAM_DEL_OTHER_CONTENT);
                List<ProductionOrderUserEntity> productionOrderUserEntityList = productionOrderUserModel.getProductionOrderUserListByProductionOrderId(corpid, dataId);
                for (ProductionOrderUserEntity item : productionOrderUserEntityList) {
                    pushUserList.add(item.getUserId());
                }
                break;
            case WORK_ORDER_TEAM:
                businessDataName = paasFormDataEntity.getSerialNo();
                businessDataName = businessDataName == null ? saveName : businessDataName;
                pushTypeEnum = PushTypeEnum.WORKORDER_TEAM;
                title = I18nMessageUtil.getMessage(WorkOrderPushConstant.WORK_ORDER_TEAM_DEL_TITLE);
                userContent = I18nMessageUtil.getMessage(WorkOrderPushConstant.TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(WorkOrderPushConstant.TEAM_DEL_OTHER_CONTENT);
                break;
            case DISTRIBUTOR_TEAM:
                businessDataName = paasFormDataEntity.getData().getString(DistributorEnum.NAME.getAttr());
                pushTypeEnum = isMain ? PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_MAIN : PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC;
                title = isMain ? I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(DistributorPushConstant.DISTRIBUTOR_TEAM_DEL_OTHER_CONTENT);
                break;
            case CLUE_TEAM:
                businessDataName = FastJsonHelper.getStringOrDefaultFromFormData(paasFormDataEntity.getData(), ClueEnum.COMPANY_NAME.getAttr(), "");
                pushTypeEnum = isMain ? PushTypeEnum.CLUE_TEAM_ADD_DEL_MAIN : PushTypeEnum.CLUE_TEAM_ADD_DEL_SYNERGETIC;
                title = isMain ? I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_DEL_MAIN_TITLE) : I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_DEL_COLLABORATOR_TITLE);
                userContent = I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_DEL_CONTENT);
                pushContent = I18nMessageUtil.getMessage(CluePushConstant.CLUE_TEAM_DEL_OTHER_CONTENT);
                break;
            default:
                break;
        }
        if (Objects.nonNull(pushTypeEnum) && !Objects.equals(userTeamEnum, UserTeamEnum.UNKNOW)) {
            businessType = XbbRefTypeEnum.transferBusinessType2Index(businessType);
            if (Objects.equals(userTeamEnum, UserTeamEnum.WORK_ORDER_TEAM)) {
                WorkOrderEntity workOrderEntity = new WorkOrderEntity();
                BeanUtil.copyProperties(paasFormDataEntity, workOrderEntity);
                pushUserList = pushHelper.getUserIdListByPushSetRule(corpid, pushTypeEnum.getCode(), pushTypeEnum.getSubCode(), operator, workOrderEntity);
            } else if (!Objects.equals(userTeamEnum, UserTeamEnum.SUPPLIER_TEAM) && !Objects.equals(userTeamEnum, UserTeamEnum.BOMBILL_TEAM) &&
                    !Objects.equals(userTeamEnum, UserTeamEnum.PRODUCTIONORDER_TEAM) && !Objects.equals(userTeamEnum, UserTeamEnum.PURCHASE_TEAM)) {
                // 从设置中获取推送人员
                pushUserList = pushHelper.getUserIdListByPushSetRule(corpid, pushTypeEnum.getCode(), pushTypeEnum.getSubCode(), operator, paasFormDataEntity);
            }

            UserVO loginUser = operator;
            if (pushUserList != null && pushUserList.size() > 0) {
                PushRefTypePojo.Options options = new PushRefTypePojo.Options(paasFormDataEntity.getAppId(), paasFormDataEntity.getMenuId(), paasFormDataEntity.getFormId(), dataId, Collections.singletonList(dataId), SaasMarkEnum.SAAS.getCode(), businessType, businessType);

                if (userTeamEnum.equals(UserTeamEnum.WORK_ORDER_TEAM)) {
                    userContent = String.format(userContent, loginUser.getName(), businessDataName);
                } else {
                    userContent = String.format(userContent, businessDataName);
                }
                if (!(delUserSet.contains(operator.getUserId()) && Objects.equals(delUserSet.size(), 1))) {
                    new ArrayList<>(delUserSet).remove(loginUser.getUserId());
                    MessageRabbitMqDTO messageRabbitMqDTO = new MessageRabbitMqDTO(corpid, new ArrayList<>(delUserSet), Collections.singletonList(dataId), 0, null, title, userContent, null, options);
                    baseProducer.sendMessage(pushTypeEnum, messageRabbitMqDTO);
                }

                Map<String, String> userIdNameMap = userModel.getUserIdNameMap(corpid, delUserSet);
                StringBuilder teamUserNameSb = new StringBuilder();
                for (String name : userIdNameMap.values()) {
                    teamUserNameSb.append(name).append(StringConstant.COMMA);
                }
                String teamUserName = teamUserNameSb.toString();
                if (StringUtil.isNotEmpty(teamUserName)) {
                    teamUserName = teamUserName.substring(0, teamUserName.length() - 1);
                }
                if (userTeamEnum.equals(UserTeamEnum.WORK_ORDER_TEAM)) {
                    pushContent = String.format(pushContent, loginUser.getName(), teamUserName, businessDataName);
                } else {
                    pushContent = String.format(pushContent, teamUserName, businessDataName);
                }
                for (String updateUserId : delUserSet) {
                    pushUserList.remove(updateUserId);
                }
                pushUserList.remove(loginUser.getUserId());
                MessageRabbitMqDTO messageRabbitMqPushSetDTO = new MessageRabbitMqDTO(corpid, pushUserList, Collections.singletonList(dataId), 0, null, title, pushContent, null, options);
                baseProducer.sendMessage(pushTypeEnum, messageRabbitMqPushSetDTO);
            }
        }


    }
}
